package com.capgemini.fs.presentation.forum.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.capgemini.fs.app.ekms.common.Constants;
import com.capgemini.fs.app.ekms.model.Forum;
import com.capgemini.fs.app.ekms.service.ForumService;
import com.capgemini.fs.app.ekms.service.GroupService;
import com.capgemini.fs.presentation.forum.web.form.CreateForumForm;

public class CreateForumAction extends DispatchAction {
	private GroupService createGroupService = null;

	public GroupService getCreateGroupService() {
		return createGroupService;
	}

	public void setCreateGroupService(GroupService createGroupService) {
		this.createGroupService = createGroupService;
	}

	private ForumService forumService = null;

	public ForumService getForumService() {
		return forumService;
	}

	public void setForumService(ForumService forumService) {
		this.forumService = forumService;
	}

	public ActionForward listStatus(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		return mapping.findForward("succ");
	}

	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		System.out.println("In action");
		CreateForumForm createForumForm = (CreateForumForm) form;
		try {
			System.out.println("In try");
			Forum forum = serializeForum(createForumForm);
			if (null == forum.getName().trim()
					|| "".equalsIgnoreCase(forum.getName().trim())) {
				
				request.setAttribute("Failed",
						"Please fill in the mandatory field Forum Name!");
				return mapping.findForward("failed");
			}
			List<Forum> forumList=forumService.getAllForums();
			for (Forum foruml : forumList) {
				if (forum.getName().trim().equalsIgnoreCase(
						foruml.getName().trim())) {
					request.setAttribute("Failed", "The " + "\'"
							+ forum.getName() + "\'"
							+ " already exist, please fill in again!");
					return mapping.findForward("failed");
				}
			}

			forumService.createForum(forum);
			request.setAttribute("Success", "Created Successfully!");
			return mapping.findForward("succ");
		} catch (Exception e) {
			request.setAttribute("Failed", e.getMessage());
			return mapping.findForward("failed");
		}
	}

	public Forum serializeForum(CreateForumForm forumForm) {
		Forum forum = new Forum();
		forum.setName(forumForm.getForumName());
		forum.setDescription(forumForm.getDescription());

		if (forumForm.getActive() == 0) {
			forum.setStatus(Constants.FORUM_STATUS_ACTIVE);
		} else {
			forum.setStatus(Constants.FORUM_STATUS_INACTIVE);
		}
		return forum;
	}

	public ActionForward load(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("succ");
	}

}
